package com.crk.yiyuanguahao.service.impl;

import com.crk.yiyuanguahao.dao.DoctorCommentDao;
import com.crk.yiyuanguahao.dao.DoctorDao;
import com.crk.yiyuanguahao.dao.OrgDao;
import com.crk.yiyuanguahao.dao.UserDao;
import com.crk.yiyuanguahao.entity.*;
import com.crk.yiyuanguahao.service.DoctorService;
import com.crk.yiyuanguahao.util.Page;
import com.crk.yiyuanguahao.util.ResponseEnum;
import com.crk.yiyuanguahao.util.ResponseResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @description:
 * @date 2020/3/26
 */
@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    DoctorDao doctorDao;
    @Autowired
    OrgDao orgDao;
    @Autowired
    DoctorCommentDao doctorCommentDao;
    @Autowired
    UserDao userDao;

    private final static String[] DEFAULT_TIMES = {"8:00-8:30","8:30-9:00"
            ,"9:00-9:30","9:30-10:00","10:00-10:30","10:30-11:00","11:00-11:30"
            ,"14:00-14:30","14:30-15:00","15:00-15:30","15:30-16:00","16:00-16:30"};

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    public ResponseResult saveDoctor(Doctor doctor) {
        if (null != doctor){
            int result = 0;
            if (doctor.getId() == null){
                doctor.setCreateTime(new Date());
                doctor.setUpdateTime(new Date());
                Doctor doctorByCode = doctorDao.selectDoctorByCode(doctor.getDoctorCode());
                if (null != doctorByCode){
                    return ResponseResult.fail(ResponseEnum.DOCTOR_EXIST_ERROR);
                }
                result = doctorDao.insert(doctor);
            }else{
                Doctor oldDoctor = doctorDao.selectByPrimaryKey(doctor.getId());
                if (!oldDoctor.getDoctorCode().equals(doctor.getDoctorCode())){
                    Doctor doctorByCode = doctorDao.selectDoctorByCode(doctor.getDoctorCode());
                    if (null != doctorByCode){
                        return ResponseResult.fail(ResponseEnum.DOCTOR_EXIST_ERROR);
                    }
                }
                doctor.setUpdateTime(new Date());
                result = doctorDao.updateByPrimaryKey(doctor);
            }

            if (result > 0){
                return ResponseResult.success(doctor);
            }else{
                return ResponseResult.fail();
            }
        }else {
            return ResponseResult.fail();
        }
    }

    @Override
    public ResponseResult deleteDoctor(Integer id) {
        int deleteByPrimaryKey = doctorDao.deleteByPrimaryKey(id);
        if (deleteByPrimaryKey >0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    @Override
    public ResponseResult getDoctor(Integer id) {
        Doctor doctor = doctorDao.selectByPrimaryKey(id);
        return ResponseResult.success(doctor);
    }

    @Override
    public ResponseResult queryDoctorPage(String doctorCode,String doctorName,Page pageInfo) {
        Integer count = doctorDao.countByCondition(doctorCode, doctorName);
        pageInfo.setAllRow(count);
        List<Doctor> roles = doctorDao.selectByCondition(doctorCode, doctorName, pageInfo.getStart(), pageInfo.getSize());
        List<DoctorDto> doctorDtos = new ArrayList<>();
        for (Doctor role : roles) {
            DoctorDto doctorDto = new DoctorDto();
            Org org = orgDao.selectByPrimaryKey(role.getOrgId());
            if (null != org){
                doctorDto.setOrgName(org.getOrgName());
            }else{
                doctorDto.setOrgName("");
            }
            doctorDto.setId(role.getId());
            doctorDto.setDoctorCode(role.getDoctorCode());
            doctorDto.setDoctorName(role.getDoctorName());
            doctorDto.setBrithday(role.getBrithday());
            doctorDto.setDoctorDesc(role.getDoctorDesc());
            doctorDto.setCreateTime(role.getCreateTime());
            doctorDto.setGender(role.getGender());
            doctorDtos.add(doctorDto);
        }
        return ResponseResult.success(doctorDtos);
    }

    @Override
    public ResponseResult queryDoctorByOrg(Integer orgId,String keyword) {
        List<Doctor> doctorList = doctorDao.queryDoctorByOrg(orgId,keyword);
        return ResponseResult.success(doctorList);
    }

    @Override
    public ResponseResult getDoctorList(int orgId) {
        List<Doctor> doctors = doctorDao.getDoctorList(orgId);
        return ResponseResult.success(doctors);
    }


    @Override
    public ResponseResult getDetailTimeList() {
        return ResponseResult.success(DEFAULT_TIMES);
    }

    @Override
    public ResponseResult getDoctorDetail(String doctorCode) {
        Doctor doctor = doctorDao.selectDoctorByCode(doctorCode);
        List<DoctorComment> doctorComments = doctorCommentDao.selectByDoctorCode(doctorCode);
        List<DoctorCommentDto> doctorCommentDtoList = new ArrayList<>();
        for (DoctorComment doctorComment : doctorComments) {
            DoctorCommentDto doctorCommentDto = new DoctorCommentDto();
            User user = userDao.selectByPrimaryKey(doctorComment.getCreator());
            doctorCommentDto.setCreatorName(user.getUserName());
            doctorCommentDto.setContent(doctorComment.getContent());
            doctorCommentDto.setCreateTime(doctorComment.getCreateTime());
            doctorCommentDto.setScore(doctorComment.getScore());
            doctorCommentDtoList.add(doctorCommentDto);
        }
        DoctorDto doctorDto = new DoctorDto();
        doctorDto.setId(doctor.getId());
        doctorDto.setDoctorName(doctor.getDoctorName());
        doctorDto.setDoctorDesc(doctor.getDoctorDesc());
        doctorDto.setDoctorCode(doctor.getDoctorCode());
        doctorDto.setDoctorCommentDtoList(doctorCommentDtoList);
        return ResponseResult.success(doctorDto);
    }
}
